#!/bin/bash
# 
# Source reading test script
#
# Copyright (C) 2010 Nikolai Kondrashov
# 
# This file is part of hidrd.
# 
# Hidrd is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# Hidrd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with hidrd; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# 

set -e -u -o pipefail
shopt -s nullglob

WRAPPER=${HIDRD_READ_TEST_WRAPPER:-}

function hexdump_col ()
{
    hexdump -v -e '1/1 "%.2X\n"' "$@"
}

function run ()
{
    local cmd="$1"; shift
    local format="$1"; shift
    local options="$1"; shift
    local input="$1"; shift
    local output="$1"; shift
    local output_basename="`basename \"$output\"`"
    local test_output="`mktemp --suffix=\"_$output_basename.test\"`"
    local txt_output="`mktemp --suffix=\"_$output_basename.txt\"`"
    local test_txt_output="`mktemp --suffix=\"_$output_basename.txt.test\"`"
    local status

    echo "Checking \"$format\" reading from \"$input\"" \
         "against \"$output\"" \
         "with options \"$options\"..." >&2

    set +e
    (
        set -e
        local output_size
        local test_output_size
        local max_size

        $WRAPPER "$cmd" "$format" "$options" "$input" "$test_output"

        output_size=`stat -c %s "$output"`
        test_output_size=`stat -c %s "$test_output"`
        if (( $test_output_size > $output_size )); then
            max_size=$test_output_size
        else
            max_size=$output_size
        fi

        hexdump_col "$output" > "$txt_output"
        hexdump_col "$test_output" > "$test_txt_output"

        diff -u -U$max_size "$txt_output" "$test_txt_output" >&2
    )
    status=$?
    set -e

    rm -f "$test_output"
    rm -f "$txt_output"
    rm -f "$test_txt_output"

    if (( $status == 0 )); then
        echo "PASSED." >&2
    else
        echo "FAILED." >&2
    fi

    return $status
}


function usage_exit ()
{
    echo "Usage: `basename \"$0\"` <format> <options> <extension> <data_dir>" >&2
    echo
    exit 1
}

FMT=${1:-}
GOPTS=${2:-}
EXT=${3:-}
DIR=${4:-${HIDRD_READ_TEST_DATA:-}}

if [ -z "$FMT" ]; then
    echo "Format is not specified." >&2
    usage_exit
fi
if [ -z "$EXT" ]; then
    echo "Extension is not specified." >&2
    usage_exit
fi
if [ -z "$DIR" ]; then
    echo "Data directory is not specified." >&2
    usage_exit
fi

HIDRD_READ="`readlink -f \"\`which hidrd_read\`\"`"

cd "$DIR"

for INPUT_FILE in *.${EXT}; do
    OUTPUT_FILE="${INPUT_FILE%.${EXT}}.bin"
    OPTS_FILE="${INPUT_FILE}.opt"
    if [ -e "$OPTS_FILE" ]; then
        SOPTS="`cat \"$OPTS_FILE\"`"
    fi

    # Join global and specific options with comma, if necessary
    OPTS="${GOPTS:-}${GOPTS:+${SOPTS:+,}}${SOPTS:-}"

    run "$HIDRD_READ" "$FMT" "$OPTS" "$INPUT_FILE" "$OUTPUT_FILE"
done


